using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

//first, find the corresponding factory.
//second, find if the factory contains the pool.
//if no, create pool first. else, the pool is responsible for deal with the object
 
public class ObjectFactoryManager : MonoBehaviour {
	private ObjectFactoryDict dict = new ObjectFactoryDict();
	private static Dictionary<string,ObjectFactory> slug2FactoryDic = new Dictionary<string, ObjectFactory>();
    public static ObjectFactoryManager instance;
	//usage
    #region public 
	public static GameObject Spawn(string slug){
        if(!slug2FactoryDic.ContainsKey(slug))
        {
            Debug.LogError("no such slug!");
            return null;
        }
        ObjectFactory factory = slug2FactoryDic[slug];
        GameObject go = factory.Spawn(slug);
        return go;
	}
	public GameObject Spawn(CharacterType type){
		return Spawn(type.ToString());
	}
	public GameObject Spawn(BounusItemType type){
		return Spawn(type.ToString());
	}
	public GameObject Spawn(EquipmentType type){
		return Spawn(type.ToString());
	}
	public GameObject Spawn(HudType type){
		return Spawn(type.ToString());
	}
    public GameObject Spawn(ParticleEffectType type){
        return Spawn(type.ToString());
    }

	public void UnSpawn(GameObject go)
    {
        ObjectFactory factory = GetFactoryByObject(go);
        factory.UnSpawn(go);
	}
    #endregion
  

    void Clear()
	{
		dict.DestroyAll();
    }
    
    void Awake(){
        instance = this;
        LoadConfigAndBuild();
		DontDestroyOnLoad(gameObject);
    }

    ObjectFactory GetFactoryByObject(GameObject go)
    {
        if(!ObjectPool.Obj2PoolDic.ContainsKey(go))
        {
            return null;
        }
        ObjectPool pool = ObjectPool.Obj2PoolDic[go];
        return pool.factory;
    }
    
    public void LoadConfigAndBuild()
    {
        string configText = null;
        string factoryPath = ObjectFactory.AssetPath + "/" + ObjectFactory.CONFIG_FILE;
        configText = System.IO.File.ReadAllText(factoryPath);

        Hashtable table = (Hashtable)MiniJSON.JsonDecode(configText);
        foreach (System.Collections.DictionaryEntry element in table)
        {
            ObjectFactory factory = null;
            if(element.Key.ToString() == "Character")
            {
            	factory = dict.Create(FactoryType.Character);
            }
            else if(element.Key.ToString() == "BounusItem")
            {
            	factory = dict.Create(FactoryType.BounusItem);
            }
            else if(element.Key.ToString() == "Equipment")
            {
            	factory = dict.Create(FactoryType.Equipment);
            }
            else if(element.Key.ToString() == "Hud")
            {
            	factory = dict.Create(FactoryType.Hud);
            }
            else if(element.Key.ToString() == "ParticleEffect")
            {
            	factory = dict.Create(FactoryType.ParticleEffect);
            }
            AddToSlug2FactoryDic(element,factory);
        }
   
    }

	void AddToSlug2FactoryDic(System.Collections.DictionaryEntry element,ObjectFactory factory)
	{
		ArrayList array = element.Value as ArrayList;
		for(int i=0;i<array.Count;i++)
		{
			slug2FactoryDic.Add(array[i].ToString(),factory);
		}
	}
       
    /*
    void OnGUI()
    {
        if(GUI.Button(new Rect(0,0,100,100),"character"))
        {
            ObjectFactoryManager.Spawn("dongzhuo");
        }
    }
    */
}








